package org.brain.bean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.brain.pojos.Images;
import org.brain.pojos.User;
import org.brain.tool.ToolBean;


@ManagedBean(name="imageList")
@RequestScoped
public class ImageList {
	User user;
	List<Images> imgList = new ArrayList<Images>();
	public ImageList() {
		// TODO Auto-generated constructor stub
		
		user = ToolBean.getUserSession();
		String path;
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		path=request.getRealPath("/");
		path = path+"/pic/"+user.getUsername();
		File file = new File(path);
		if(!file.exists())
		{
			file.mkdirs();
		}
		String[] filePath = file.list();
		if(filePath.length > 0)
		{
			for(String str : filePath)
				imgList.add(new Images(str, "pic/"+user.getUsername()+"/"+str));
		}
			
	}
	
	public void updateList()
	{
		String path;
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		path=request.getRealPath("/");
		path = path+"/pic/"+user.getUsername();
		File file = new File(path);
		String[] filePath = file.list();
		if(filePath.length > 0)
		{
			imgList.clear();
			for(String str : filePath)
				imgList.add(new Images(str, "pic/"+user.getUsername()+"/"+str));
		}
		System.out.println("123123");
	}
	
	
	public String FilePath()
	{
		return "testResult";
	}


	public List<Images> getImgList() {
		return imgList;
	}


	public void setImgList(List<Images> imgList) {
		this.imgList = imgList;
	}
	
	
	
}
